<template>
    <div class="login-container">
        <div class="login-box">
            <!-- 头像区域 -->
            <div class="avatar-box">
                <img class="avatar" src="../assets/logo.svg" />
            </div>
            <!-- 表单区域 -->
            <div class="form-login">
                <!-- 账号 -->
                <div class="form-group">
                    <label for="username">账号：</label>
                    <input type="text" class="form-control" 
                        id="username" placeholder="请输入用户名" 
                        autocomplete="off"
                        v-model.trim.lazy="username" />
                </div>
                <!-- 密码 -->
                <div class="form-group">
                    <label for="password">密码：</label>
                    <input type="password" class="form-control" id="password" placeholder="请输入登录密码"
                        v-model.trim.lazy="password" />
                </div>
                <!-- 登录 -->
                <div class="form-group">
                    <button type="button" class="btn"
                    @click="toLogin">登录</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const username = ref('')
const password = ref('')
const userList = [
    { id: 1, username: 'admin', password: '123456' },
    { id: 2, username: 'zhangsan', password: '123456' },
    { id: 3, username: 'lisi', password: '123456' },
]
const toLogin = () => {
    // 收集2个输入框的数据跟预定义的数据比较
    let user = userList.find(item => item.username === username.value 
        && item.password === password.value)
    if(user) {
        // 登录成功user.username === username.value
        sessionStorage.setItem('token', user.username)
        router.push('/home')
    } else {
        // 做提示
        alert('用户名或密码错误')
    }
}
</script>

<style scoped>
.login-container {
    background-color: #35495e;
    height: 100%;

    .login-box {
        width: 400px;
        height: 250px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);

        .form-login {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            box-sizing: border-box;
            padding: 1.5rem !important;

            .form-group {
                padding-top: 20px;

                .btn {
                    background-color: deepskyblue;
                    color: #fff;
                }
            }
        }
    }
}

.form-control {
    flex: 1;
    padding: 0.5rem;
}

.avatar-box {
    position: absolute;
    width: 100%;
    top: -65px;
    left: 0;

    .avatar {
        width: 120px;
        height: 120px;
        border-radius: 50% !important;
        box-shadow: 0 0 6px #efefef;
        background-color: #fff;
    }
}
</style>